#include "editoritemfactory.h"

using namespace core;
using namespace swp;

EditorItemFactory* editorItemFactory = 0;

EditorItemFactory* EditorItemFactory::getInstance()
{
    if (editorItemFactory == 0)
    {
        editorItemFactory = new EditorItemFactory();
    }

    return editorItemFactory;
}

void EditorItemFactory::destroy()
{
    delete editorItemFactory;
    editorItemFactory = 0;
}

void EditorItemFactory::registerEditorItem(int type, core::IEditorItem* item)
{
    if (!m_registeredEditorItems.contains(type))
    {
        m_registeredEditorItems[type] = item->copy();
    }
}

void EditorItemFactory::unregisterEditorItem(int type)
{
    if (m_registeredEditorItems.contains(type))
    {
        IEditorItem* item =
            m_registeredEditorItems[type];
        m_registeredEditorItems.remove(type);

        delete item;
    }
}

IEditorItem* EditorItemFactory::editorItem(int type) const
{
    IEditorItem* ret = 0;

    if (m_registeredEditorItems.contains(type))
    {
        ret = m_registeredEditorItems[type]->copy();
    }

    return ret;
}

EditorItemFactory::EditorItemFactory() : m_registeredEditorItems()
{

}

EditorItemFactory::~EditorItemFactory()
{
    foreach (IEditorItem* item, m_registeredEditorItems)
    {
        delete item;
    }
}
